using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Xml.Serialization;
using System.Diagnostics;

namespace MobileClient
{
	/// <summary>
	/// Summary description for frmUploadForm.
	/// </summary>
	public class frmUploadForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label lblStatus;
		private System.Windows.Forms.ProgressBar pbarStatus;
		private System.Windows.Forms.PictureBox pbLocation1;
		private System.Windows.Forms.PictureBox pbServer;
		private System.Windows.Forms.PictureBox pbPDA;
		private System.Windows.Forms.PictureBox pbLocation5;
		private System.Windows.Forms.PictureBox pbLocation4;
		private System.Windows.Forms.PictureBox pbLocation3;
		private System.Windows.Forms.PictureBox pbLocation2;
		private bool bUploading;
		private System.Windows.Forms.PictureBox pbConnectingC;
		private System.Windows.Forms.PictureBox pbConnectingB;
		private System.Windows.Forms.PictureBox pbConnectingA;
		private int m_nCurrentPos = -1;
	
		public frmUploadForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			// set bar location
			m_nCurrentPos = -1;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmUploadTemplates));
            this.lblStatus = new System.Windows.Forms.Label();
            this.pbarStatus = new System.Windows.Forms.ProgressBar();
            this.pbLocation5 = new System.Windows.Forms.PictureBox();
            this.pbLocation4 = new System.Windows.Forms.PictureBox();
            this.pbLocation3 = new System.Windows.Forms.PictureBox();
            this.pbLocation2 = new System.Windows.Forms.PictureBox();
            this.pbLocation1 = new System.Windows.Forms.PictureBox();
            this.pbServer = new System.Windows.Forms.PictureBox();
            this.pbPDA = new System.Windows.Forms.PictureBox();
            this.pbConnectingC = new System.Windows.Forms.PictureBox();
            this.pbConnectingB = new System.Windows.Forms.PictureBox();
            this.pbConnectingA = new System.Windows.Forms.PictureBox();
            this.SuspendLayout();
            // 
            // lblStatus
            // 
            this.lblStatus.Location = new System.Drawing.Point(8, 156);
            this.lblStatus.Name = "lblStatus";
            this.lblStatus.Size = new System.Drawing.Size(224, 52);
            this.lblStatus.Text = "Idle";
            this.lblStatus.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // pbarStatus
            // 
            this.pbarStatus.Location = new System.Drawing.Point(8, 220);
            this.pbarStatus.Name = "pbarStatus";
            this.pbarStatus.Size = new System.Drawing.Size(224, 20);
            // 
            // pbLocation5
            // 
            this.pbLocation5.Image = ((System.Drawing.Image)(resources.GetObject("pbLocation5.Image")));
            this.pbLocation5.Location = new System.Drawing.Point(153, 48);
            this.pbLocation5.Name = "pbLocation5";
            this.pbLocation5.Size = new System.Drawing.Size(14, 71);
            this.pbLocation5.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.pbLocation5.Visible = false;
            // 
            // pbLocation4
            // 
            this.pbLocation4.Image = ((System.Drawing.Image)(resources.GetObject("pbLocation4.Image")));
            this.pbLocation4.Location = new System.Drawing.Point(133, 48);
            this.pbLocation4.Name = "pbLocation4";
            this.pbLocation4.Size = new System.Drawing.Size(14, 71);
            this.pbLocation4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.pbLocation4.Visible = false;
            // 
            // pbLocation3
            // 
            this.pbLocation3.Image = ((System.Drawing.Image)(resources.GetObject("pbLocation3.Image")));
            this.pbLocation3.Location = new System.Drawing.Point(113, 48);
            this.pbLocation3.Name = "pbLocation3";
            this.pbLocation3.Size = new System.Drawing.Size(14, 71);
            this.pbLocation3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.pbLocation3.Visible = false;
            // 
            // pbLocation2
            // 
            this.pbLocation2.Image = ((System.Drawing.Image)(resources.GetObject("pbLocation2.Image")));
            this.pbLocation2.Location = new System.Drawing.Point(93, 48);
            this.pbLocation2.Name = "pbLocation2";
            this.pbLocation2.Size = new System.Drawing.Size(14, 71);
            this.pbLocation2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.pbLocation2.Visible = false;
            // 
            // pbLocation1
            // 
            this.pbLocation1.Image = ((System.Drawing.Image)(resources.GetObject("pbLocation1.Image")));
            this.pbLocation1.Location = new System.Drawing.Point(73, 48);
            this.pbLocation1.Name = "pbLocation1";
            this.pbLocation1.Size = new System.Drawing.Size(14, 71);
            this.pbLocation1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
            this.pbLocation1.Visible = false;
            // 
            // pbServer
            // 
            this.pbServer.Image = ((System.Drawing.Image)(resources.GetObject("pbServer.Image")));
            this.pbServer.Location = new System.Drawing.Point(173, 48);
            this.pbServer.Name = "pbServer";
            this.pbServer.Size = new System.Drawing.Size(58, 71);
            // 
            // pbPDA
            // 
            this.pbPDA.Image = ((System.Drawing.Image)(resources.GetObject("pbPDA.Image")));
            this.pbPDA.Location = new System.Drawing.Point(9, 48);
            this.pbPDA.Name = "pbPDA";
            this.pbPDA.Size = new System.Drawing.Size(58, 71);
            // 
            // pbConnectingC
            // 
            this.pbConnectingC.Image = ((System.Drawing.Image)(resources.GetObject("pbConnectingC.Image")));
            this.pbConnectingC.Location = new System.Drawing.Point(96, 48);
            this.pbConnectingC.Name = "pbConnectingC";
            this.pbConnectingC.Size = new System.Drawing.Size(44, 71);
            this.pbConnectingC.Visible = false;
            // 
            // pbConnectingB
            // 
            this.pbConnectingB.Image = ((System.Drawing.Image)(resources.GetObject("pbConnectingB.Image")));
            this.pbConnectingB.Location = new System.Drawing.Point(122, 48);
            this.pbConnectingB.Name = "pbConnectingB";
            this.pbConnectingB.Size = new System.Drawing.Size(32, 71);
            // 
            // pbConnectingA
            // 
            this.pbConnectingA.Image = ((System.Drawing.Image)(resources.GetObject("pbConnectingA.Image")));
            this.pbConnectingA.Location = new System.Drawing.Point(86, 48);
            this.pbConnectingA.Name = "pbConnectingA";
            this.pbConnectingA.Size = new System.Drawing.Size(32, 71);
            // 
            // frmUploadTemplates
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            this.ClientSize = new System.Drawing.Size(238, 295);
            this.Controls.Add(this.pbConnectingC);
            this.Controls.Add(this.pbConnectingB);
            this.Controls.Add(this.pbConnectingA);
            this.Controls.Add(this.pbLocation5);
            this.Controls.Add(this.pbLocation4);
            this.Controls.Add(this.pbLocation3);
            this.Controls.Add(this.pbLocation2);
            this.Controls.Add(this.pbLocation1);
            this.Controls.Add(this.pbServer);
            this.Controls.Add(this.pbPDA);
            this.Controls.Add(this.pbarStatus);
            this.Controls.Add(this.lblStatus);
            this.Name = "frmUploadTemplates";
            this.Text = "Upload Form";
            this.Load += new System.EventHandler(this.frmUploadForm_Load);
            this.ResumeLayout(false);

		}
		#endregion

        public MobileClient.UIProcessService.Form UploadForm(MobileClient.UIProcessService.Form theForm)
		{
			// string strForm;
            // string strFormName;
            // string[] strFiles = null;
			string strCompany;
			string strUserID;
			string strPassword;
			string strProtocol;
			string strServer;
			string strPort;
			bool bUseProxy;
			string strProxyServerUri;
			string strProxyUser;
			string strProxyPassword;
			StringBuilder strURL = new StringBuilder();
			string strPath = SettingValues.GetResponsePath();
            MobileClient.UIProcessService.Form frmResult = null;

			if(!bUploading)
			{
				Cursor.Current = Cursors.WaitCursor;

				bUploading = true;
				try
				{
					string strSettingsPath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase + ".config";

					Settings objSettings = new Settings();
					objSettings.FilePath = strSettingsPath;
					objSettings.Read();
					strCompany = objSettings.Company;
					strUserID = objSettings.UserID;
					strPassword = objSettings.Password;
					strProtocol = objSettings.Protocol;
					strServer = objSettings.Server;
					strPort = objSettings.Port;
					bUseProxy= objSettings.UseProxy;
					strProxyServerUri= objSettings.ProxyServer;
					strProxyUser= objSettings.ProxyUserID;
					strProxyPassword= objSettings.ProxyPassword;
					strPath = SettingValues.GetResponsePath();

					pbarStatus.Maximum = 100;
					pbarStatus.Value = 0;
					lblStatus.Text = "Connecting to web service.";
					pbConnectingA.Visible = false;
					pbConnectingB.Visible = false;
					pbConnectingC.Visible = true;
					Thread.Sleep(1000);
					Application.DoEvents();
					pbConnectingA.Visible = true;
					pbConnectingB.Visible = true;
					pbConnectingC.Visible = false;
					Thread.Sleep(1000);
					Application.DoEvents();
					pbConnectingA.Visible = false;
					pbConnectingB.Visible = false;
					pbConnectingC.Visible = true;
					Thread.Sleep(1000);
					Application.DoEvents();
					pbConnectingA.Visible = true;
					pbConnectingB.Visible = true;
					pbConnectingC.Visible = false;
					Thread.Sleep(1000);
					Application.DoEvents();
					pbConnectingA.Visible = false;
					pbConnectingB.Visible = false;
					pbConnectingC.Visible = true;
					Thread.Sleep(1000);
					Application.DoEvents();

                    // connect to the web service and download the template list
                    MobileClient.UIProcessService.theService wsForm = new MobileClient.UIProcessService.theService();
					
                    // use the endpoint address specified in the FormAction
					wsForm.Url = theForm.FormAction.Endpoint;
					// configure a web proxy if selected
					if(bUseProxy)
					{
						WebProxy objProxy = new WebProxy();
						Uri objUri = new Uri(strProxyServerUri);
						objProxy.Address = objUri;
						objProxy.Credentials=new NetworkCredential(strProxyUser, strProxyPassword);
						wsForm.Proxy = objProxy;
					}
					
					// update status
					lblStatus.Text = "Uploading form response " + theForm.Name;
					pbarStatus.Value++;
					IncrementBar();
					Application.DoEvents();

                    // create a UploadForm Request
                    MobileClient.UIProcessService.ProcessFormRequest theRequest = new MobileClient.UIProcessService.ProcessFormRequest();
                    theRequest.Credentials = new MobileClient.UIProcessService.Credentials();
                    theRequest.Credentials.Company = strCompany;
                    theRequest.Credentials.UserID = strUserID;
                    theRequest.Credentials.Password = strPassword;
                    theRequest.TheForm = theForm;

                    // upload the form
                    frmResult = wsForm.ProcessForm(theRequest);

					lblStatus.Text = "Form response " + theForm.Name + " uploaded";
					IncrementBar();
					Application.DoEvents();
                    Thread.Sleep(1000);
                }
				catch(Exception ex)
				{
					MessageBox.Show(ex.Message);
				}

				lblStatus.Text = "Upload complete.";
				pbarStatus.Value++;
				HideBar();
				Application.DoEvents();

				Cursor.Current = Cursors.Default;
				this.Close();
			}

            return frmResult;
        }

		private void IncrementBar()
		{
			m_nCurrentPos++;
			switch(m_nCurrentPos)
			{
				case 0:
					pbLocation1.Visible = true;
					pbLocation2.Visible = false;
					pbLocation3.Visible = false;
					pbLocation4.Visible = false;
					pbLocation5.Visible = false;
					break;
				case 1:
					pbLocation1.Visible = false;
					pbLocation2.Visible = true;
					pbLocation3.Visible = false;
					pbLocation4.Visible = false;
					pbLocation5.Visible = false;
					break;
				case 2:
					pbLocation1.Visible = false;
					pbLocation2.Visible = false;
					pbLocation3.Visible = true;
					pbLocation4.Visible = false;
					pbLocation5.Visible = false;
					break;
				case 3:
					pbLocation1.Visible = false;
					pbLocation2.Visible = false;
					pbLocation3.Visible = false;
					pbLocation4.Visible = true;
					pbLocation5.Visible = false;
					break;
				case 4:
					pbLocation1.Visible = false;
					pbLocation2.Visible = false;
					pbLocation3.Visible = false;
					pbLocation4.Visible = false;
					pbLocation5.Visible = true;
					m_nCurrentPos = -1;
					break;
			}

			Application.DoEvents();
		}

		private void HideBar()
		{
			pbLocation1.Visible = false;
			pbLocation2.Visible = false;
			pbLocation3.Visible = false;
			pbLocation4.Visible = false;
			pbLocation5.Visible = false;

			Application.DoEvents();
		}

		private void frmUploadForm_Load(object sender, EventArgs e)
		{
		}

        private void SaveFormTemplate(MobileClient.UIProcessService.Form theForm, string strFileName)
        {
            try
            {
                // Create an instance of the XmlSerializer class;
                // specify the type of object to serialize.
                XmlSerializer serializer =
                new XmlSerializer(theForm.GetType());
                TextWriter writer = new StreamWriter(strFileName);
                // Serialize the Form, and close the TextWriter.
                serializer.Serialize(writer, theForm);
                writer.Close();

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
    }
}
